package org.example.uuadb.config;

import com.alibaba.fastjson.JSONObject;
import org.example.uuadb.entity.OauthClientDetail;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.*;

/**
 * @author : yurui
 * @date : 2024/6/6
 * @TIME : 16:13
 * @project : AuthDemo
 */
public class DBClientDetails implements ClientDetails {

    private final OauthClientDetail oauthClientDetail;

    public DBClientDetails(OauthClientDetail oauthClientDetail){

        this.oauthClientDetail = oauthClientDetail;
    }

    @Override
    public String getClientId() {
        return oauthClientDetail.getClientId();
    }

    @Override
    public Set<String> getResourceIds() {
        if (oauthClientDetail.getResourceIds() == null || oauthClientDetail.getResourceIds().isEmpty()){
            return Collections.emptySet();
        }
        return new HashSet<>(Arrays.asList(oauthClientDetail.getResourceIds().split(",")));
    }

    @Override
    public boolean isSecretRequired() {
        return oauthClientDetail.getClientSecret() != null;
    }

    @Override
    public String getClientSecret() {
        return oauthClientDetail.getClientSecret();
    }

    @Override
    public boolean isScoped() {
        return oauthClientDetail.getScope() != null && !oauthClientDetail.getScope().isEmpty();
    }

    @Override
    public Set<String> getScope() {
        if (oauthClientDetail.getScope() == null || oauthClientDetail.getScope().isEmpty()){
            return Collections.emptySet();
        }
        return new HashSet<>(Arrays.asList(oauthClientDetail.getScope().split(",")));
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if (oauthClientDetail.getAuthorizedGrantTypes() == null || oauthClientDetail.getAuthorizedGrantTypes().isEmpty()){
            return Collections.emptySet();
        }
        return new HashSet<>(Arrays.asList(oauthClientDetail.getAuthorizedGrantTypes().split(",")));
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        if (oauthClientDetail.getWebServerRedirectUri() == null || oauthClientDetail.getWebServerRedirectUri().isEmpty()){
            return Collections.emptySet();
        }
        return new HashSet<>(Arrays.asList(oauthClientDetail.getWebServerRedirectUri().split(",")));
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        //暂时线返回空
        return new ArrayList<>();
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return oauthClientDetail.getAccessTokenValidity();
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return oauthClientDetail.getRefreshTokenValidity();
    }

    @Override
    public boolean isAutoApprove(String s) {
        return oauthClientDetail.getAutoApprove() == 1;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return JSONObject.parseObject(oauthClientDetail.getAdditionalInformation());
    }
}
